package jp.ac.u_tokyo.dch.action;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.service.TreeService;
import jp.ac.u_tokyo.dch.util.common.ModoruUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;

/**
* @version 1.1
*/
@ParentPackage("dch-default")
public class MainAction extends ActionSupport implements DaoServiceAware{


	/**
	 * ロガー
	 */
	public Logger logger = Logger.getLogger(getClass());

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	/**
	 * 戻るURL
	 */
	private String modoruUrl;
	/**
	 *
	 */
	private static final long serialVersionUID = -3439951188264134397L;
	/**
	 * ログイン処理を行う
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * TopicMap管理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String tmMain() throws Exception{
		return SUCCESS;
	}
	/**
	 * 戻る処理を行う
	 * @return 結果
	 * @throws Exception
	 */
	public String modoru() throws Exception {

		modoruUrl = ModoruUtil.modoruUrl(ServletActionContext.getRequest());
		return SUCCESS;
	}
	/**
	 * ツリーバッチを起動する
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String runBatch() throws Exception{

		 try {
//			 List<String> command = null;
//			 String osName = System.getProperty("os.name");
//
//			 command = new ArrayList<String>(10);
//			 if(osName.toLowerCase().startsWith("window")) {
//				 command.add("cmd.exe");
//				 command.add("/c");
//			 }
//			 command.add("sh");
//			 command.add(getText("groupingBatch"));
//
//			DchCommonUtil.exec((String[])command.toArray(new String[1]));
//			FrntTreeGenJob treeGenJob = new FrntTreeGenJob();
//			String stat = treeGenJob.exec();
//			addActionMessage(stat);
			 TreeService service = new TreeService(dbConnection);
			 service.makeTree();

			addActionMessage("現物資料グルーピング処理が正常終了しました。");
		 } catch (Exception e) {
			addActionError(getText("errMsg",new String[]{"現物資料グルーピング"}));
			logger.error(e);
		 }
		return SUCCESS;
	}

	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}

	public String getModoruUrl() {
		return modoruUrl;
	}

	public void setModoruUrl(String modoruUrl) {
		this.modoruUrl = modoruUrl;
	}


}
